class Rectangle {
  PVector[] vertices;
  PVector c;
  float w, h;
  PMatrix2D transformation;

  Rectangle() {
    this.vertices = new PVector[4];
  }

  Rectangle(Rectangle other) {
    this.vertices = new PVector[4];
    for (int i = 0; i < this.vertices.length; ++i) {
      this.vertices[i] = other.vertices[i].get();
    }
    this.c = other.c.get();
    this.w = other.w;
    this.h = other.h;
    //this.transformation = other.transformation.get();
  }

  void set(PVector v[]) {
    if (v.length < 4) return;
    // tarvitseeko järjestää?
    for (int i = 0; i < this.vertices.length; ++i) {
      this.vertices[i] = v[i];
    }
    update();
  }

  void set(float left, float right, float top, float bottom) {
    this.vertices[0] = new PVector(left, top);
    this.vertices[1] = new PVector(right, top);
    this.vertices[2] = new PVector(right, bottom);
    this.vertices[3] = new PVector(left, bottom);
    update();
  }

  void update() {
    this.w = this.vertices[0].dist(this.vertices[1]);
    this.h = this.vertices[1].dist(this.vertices[2]);
    this.c = PVector.mult(PVector.add(this.vertices[0], this.vertices[2]), 0.5);
  }

  float area() {
    return w * h;
  }

  void translate(PVector t) {
    for (PVector v : this.vertices) {
      v.add(t);
    }
    this.c.add(t);
  }

  void rotate(float angle) {
    for (PVector v : this.vertices) {
      v.rotate(angle);
    }
    this.c.rotate(angle);
  }

  void draw() {
    pushStyle();
    stroke(0, 255, 0);
    noFill();
    beginShape();
    for (PVector v : this.vertices) {
      vertex(v.x, v.y);
    }
    endShape(CLOSE);
    line(this.c.x - 10, this.c.y - 10, this.c.x + 10, this.c.y + 10);
    line(this.c.x - 10, this.c.y + 10, this.c.x + 10, this.c.y - 10);
    popStyle();
  }
}

